package com.mayikt;

/**
 * @Author: maplefire
 * @Description: threadLocal示例
 * @Date: 2022/06/17 16:49
 */

class ThreadLocalRes {

    //public Integer count =0 ;
    //public Integer getNumber(){
    //    return ++count;
    //}

    public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {

        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public Integer getNumber() {
        Integer count = threadLocal.get() + 1;
        threadLocal.set(count);
        return count;
    }
}


public class Test04 extends Thread {

    private ThreadLocalRes res;

    private Test04(ThreadLocalRes res) {
        this.res = res;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(Thread.currentThread().getName() + "," + res.getNumber());
        }
    }

    public static void main(String[] args) {
        ThreadLocalRes res = new ThreadLocalRes();
        Test04 t1 = new Test04(res);
        Test04 t2 = new Test04(res);
        t1.start();
        t2.start();
    }
}
